package ViewListener;

import java.awt.Color;
import java.util.Enumeration;

import javax.swing.JTextPane;
import javax.swing.text.AttributeSet;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;


public class SqlEditorThread implements Runnable{

	private Thread t;
	private DefaultStyledDocument doc;
	private JTextPane editor;
	Style commentStyle;
	Style normalStyle;
	Style textStyle;
	Style keywordStyle; 
	
	public SqlEditorThread(JTextPane editor, DefaultStyledDocument doc)
	{
		this.doc = doc;
		this.editor = editor;
		commentStyle = editor.addStyle("Comment", null);
		commentStyle.addAttribute(StyleConstants.Foreground, Color.getHSBColor(138.0f/360.0f, 95.0f/100.0f, 84.0f/100.0f));
		commentStyle.addAttribute(StyleConstants.FontSize, new Integer(12));
		commentStyle.addAttribute(StyleConstants.Bold,  false);
		commentStyle.addAttribute(StyleConstants.FontFamily, "arial");
		
		normalStyle = editor.addStyle("Normal", null);
		normalStyle.addAttribute(StyleConstants.Foreground, Color.BLACK);
		normalStyle.addAttribute(StyleConstants.FontSize, new Integer(12));
		normalStyle.addAttribute(StyleConstants.FontFamily, "arial");
		normalStyle.addAttribute(StyleConstants.Bold,  false);
		
		textStyle = editor.addStyle("Text", null);
		textStyle.addAttribute(StyleConstants.Foreground, Color.getHSBColor(0.0f/360.0f, 92.0f/100.0f, 94.0f/100.0f));
		textStyle.addAttribute(StyleConstants.FontSize, new Integer(12));
		textStyle.addAttribute(StyleConstants.Bold,  false);
		textStyle.addAttribute(StyleConstants.FontFamily, "arial");
		
		keywordStyle = editor.addStyle("Keyword", null);		
		keywordStyle.addAttribute(StyleConstants.Foreground, Color.getHSBColor(220.0f/360.0f, 100.0f/100.0f, 91.0f/100.0f));
		keywordStyle.addAttribute(StyleConstants.FontSize, new Integer(12));
		keywordStyle.addAttribute(StyleConstants.Bold,  false);
		keywordStyle.addAttribute(StyleConstants.FontFamily, "arial");
				
		
		t = new Thread(this, "SqlEditor");
		t.start();
	}
	
	
	@Override
	public void run() {
		// TODO Auto-generated method stub
		SqlHighlightChecker checker = new SqlHighlightChecker();
		
		while(true)
		{
			checker.CheckForHighLight(this, editor.getText());
			synchronized (this) {
				try {
					this.wait();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

	public void setComment(int index, int length)
	{
		doc.setCharacterAttributes(index, length, commentStyle, false);		
	}
	
	public void setNormal(int index, int length)
	{
		doc.setCharacterAttributes(index, length, normalStyle, false);
	}
	
	public void setText(int index, int length)
	{
		doc.setCharacterAttributes(index, length, textStyle, false);
	}
	
	public void setKeyword(int index, int length)
	{
		AttributeSet attrs = doc.getCharacterElement(index).getAttributes();
		if(attrs.getAttribute(StyleConstants.Foreground) ==	normalStyle.getAttribute(StyleConstants.Foreground))		
			doc.setCharacterAttributes(index, length, keywordStyle, false);
	}
	
	public boolean isCommentStyle(int index)
	{
		AttributeSet attrs = doc.getCharacterElement(index).getAttributes();
		if(attrs.getAttribute(StyleConstants.Foreground) ==	commentStyle.getAttribute(StyleConstants.Foreground))
			return true;
		
		return false;
	}
}
